﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DataModel.DataModelContext;

namespace DataModel.DataCenter
{
    public class GetLoginInfo
    {
        private const string ConnectionString = @"isostore:/EnglishStudy.sdf";

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="stt"></param>
        public void DeleteUser(int stt)
        {
            try
            {

                using (var context = new EnglishStudyContext(ConnectionString))
                {
                    var query = (from d in context.TblLoginInfos
                                 where d.Stt == stt
                                 select d);
                    if (query.Any())
                    {
                        context.TblLoginInfos.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + " offline home");
            }
        }

        /// <summary>
        /// Add new User Into Database
        /// </summary>
        /// <param name="login"></param>
        public void AddLoginInfo(TblLoginInfo login)
        {
            try
            {
                using (var context = new EnglishStudyContext(ConnectionString))
                {
                    // create a new country and add it to the context
                    var logininfo = new TblLoginInfo();

                    logininfo.PhoneNumber = login.PhoneNumber;
                    logininfo.NickName = login.NickName;
                    logininfo.Email = login.Email;
                    logininfo.Password = login.Password;
                    logininfo.Code = login.Code;
                    logininfo.Country = login.Country;
                    logininfo.Facebookid = login.Facebookid;
                    logininfo.IsLoginFacebook = login.IsLoginFacebook;
                    logininfo.NameFacebook = login.NameFacebook;
                    logininfo.AccessTokenFb = login.AccessTokenFb;
                    logininfo.AccessTokenTw = login.AccessTokenTw;
                    // save changes to the database
                    context.TblLoginInfos.InsertOnSubmit(logininfo);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + " offline home");
            }
        }
        /// <summary>
        /// Add new User Into Database
        /// </summary>
        /// <param name="login"></param>
        public void UpdateLoginInfo(TblLoginInfo login)
        {
            using (var context = new EnglishStudyContext(ConnectionString))
            {
                // find a LoginInfo to update
                IQueryable<TblLoginInfo> loginInfoQuery = from c in context.TblLoginInfos
                                                       where c.Stt==login.Stt
                                                       select c;
                var loginInfoToUpdate = loginInfoQuery.FirstOrDefault();
                // update the LoginInfo by changing its name
                if (loginInfoToUpdate != null)
                {
                    if (login.NickName != null)
                        loginInfoToUpdate.NickName = login.NickName;
                    if (login.PhoneNumber != null)
                        loginInfoToUpdate.PhoneNumber = login.PhoneNumber;
                    if (login.Email != null)
                        loginInfoToUpdate.Email = login.Email;
                    if (login.Password != null)
                        loginInfoToUpdate.Password = login.Password;
                    if (login.Facebookid != null)
                        loginInfoToUpdate.Facebookid = login.Facebookid;
                    if (login.NameFacebook != null)
                        loginInfoToUpdate.NameFacebook = login.NameFacebook;
                    if (login.AccessTokenFb != null)
                        loginInfoToUpdate.AccessTokenFb = login.AccessTokenFb;
                    if (login.AccessTokenTw != null)
                        loginInfoToUpdate.AccessTokenTw = login.AccessTokenTw;
                    if(login.Code!=null)
                        loginInfoToUpdate.Code = login.Code;
                    if(login.Country!=null)
                        loginInfoToUpdate.Country = login.Country;
                }
                // save changes to the database
                context.SubmitChanges();
            }
        }
        /// <summary>
        /// Get Info
        /// </summary>
        /// <returns></returns>
        public TblLoginInfo LoginInfo()
        {
            TblLoginInfo loginInfoList = null;
            try
            {
                using (var context = new EnglishStudyContext(ConnectionString))
                {
                    IQueryable<TblLoginInfo> query = from c in context.TblLoginInfos select c;
                    loginInfoList = query.FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message + " checkDB");
            }
            return loginInfoList;
        }
    }
}

